package com.fly.caihong.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.fly.caihong.R;
import com.fly.caihong.base.BaseActivity;
import com.fly.caihong.model.GoodsDetailItem;
import com.fly.caihong.utils.DataHandler;
import com.fly.caihong.utils.GlideUtils;
import com.fly.caihong.utils.StatusBarUtils;
import com.fly.caihong.utils.ToastUtils;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;

import java.util.Arrays;

public class GoodsDetailActivity extends BaseActivity {

    @Override
    protected int getLayout() {
        return R.layout.activity_goods_detail;
    }

    public static void launchActivity(Context context, String id) {
        Intent itt = new Intent(context, GoodsDetailActivity.class);
        itt.putExtra("id", id);
        context.startActivity(itt);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        StatusBarUtils.setTransparent(this);
        super.onCreate(savedInstanceState);
        StatusBarUtils.assistNavigationBar(this, findViewById(R.id.fl_root));
    }

    @Override
    protected void initViews() {
        super.initViews();
        findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        GoodsDetailItem goodsDetailItem = DataHandler.convertJsonData("home/mall/detail"
                , getIntent().getStringExtra("id") + ".json", GoodsDetailItem.class);
        if (goodsDetailItem == null) {
            ToastUtils.showLong("商品详情数据为空");
            return;
        }
        Banner banner = findViewById(R.id.banner);
        banner.setImages(Arrays.asList(goodsDetailItem.getImages().split("@")));
        banner.setImageLoader(new ImageLoader() {
            @Override
            public void displayImage(Context context, Object path, ImageView imageView) {
                GlideUtils.loadSrcImage(context, imageView, path.toString());
            }
        });
        banner.setDelayTime(3000);
        banner.start();
        TextView tvName = findViewById(R.id.tv_name);
        tvName.setText(goodsDetailItem.getGoodsName());
        TextView tvPriceNow = findViewById(R.id.tv_price_now);
        tvPriceNow.setText(goodsDetailItem.getPriceNow());
        TextView tvPriceSrc = findViewById(R.id.tv_price_src);
        tvPriceSrc.setText(goodsDetailItem.getPriceSrc());
        tvPriceSrc.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG); //中划线
        TextView tvSale = findViewById(R.id.tv_sale);
        tvSale.setText(String.format("月销%d笔", goodsDetailItem.getSaleCount()));
        TextView tvCity = findViewById(R.id.tv_city);
        tvCity.setText(goodsDetailItem.getMallCity());
    }

}
